---@class UIGuildGift : UIBase
UIGuildGift = UIBase.New(PanelResNames.UIGuildGift)

function UIGuildGift.Init(func)
    UIBase.Init(UIGuildGift, func)
end

function UIGuildGift:OnCreate()
    self.btnBack = self:GetChild("bgClose")
    self.txtBack = self:GetChild("backtext")
    self.txtNext = self:GetChild("nexttext")
    self.ctrlShowLeft = self:GetController("leftshow")
    self.ctrlShowRight = self:GetController("rightshow")
    self.listGiftItem = self:GetChild("list_guildgift")
    self.listGiftItem.itemRenderer = UIGuildGift.GiftItemRender
    --self:SetDynamicList(self.listGiftItem,self.GiftItemRender)
    self.countDownTimer = {}
    self.txtCountDown = {}
    self.countTime = {}
    self.curIndex = 1
    self:AddEvent()
    self:Refresh()
end

function UIGuildGift:AddEvent()
    self:RegisterOnClick(self.btnBack,self.ClickClose)
    self:RegisterOnClick(self:GetChild("left"),self.OnScrollToLast)
    self:RegisterOnClick(self:GetChild("right"),self.OnScrollToNext)
    self:RegisterEvent(Config.EventType.GuildGiftRefresh, self.Refresh);
    self.listGiftItem.scrollPane.onScrollEnd:Add(function ()
        if self.curIndex ~= self.listGiftItem.scrollPane.currentPageX + 1 then
            self.curIndex = self.listGiftItem.scrollPane.currentPageX + 1;
            self:UpdatePage();
        end
    end)
end

function UIGuildGift.GiftItemRender(index, go)
    local srvData = UIGuildGift.srvData[index+1]
    local cfgData = dataTable.getDataTableByStringId("t_guildgift", UIGuildGift.srvData[index+1].giftId)
    if srvData and cfgData then
        local isLeader = logicMgr.GuildManager.isChairman() or logicMgr.GuildManager.isViceChairman()
        local ctrlState = go:GetController("status")
        local listItems = go:GetChild("list")
        local txtDesc = go:GetChild("description")
        local btnBuy = go:GetChild("buy")
        local btnGet = go:GetChild("get")
        local txtDiamondCost = btnBuy:GetChild("n9")
        local txtRMBCost = btnBuy:GetChild("title")
        local ctrlBuyType = btnBuy:GetController("Type")
        UIGuildGift.txtCountDown[cfgData.f_GuildGiftId] = go:GetChild("countDown")
        UIGuildGift.countTime[cfgData.f_GuildGiftId] = 0
        if cfgData.f_GuildGiftBuyTpye == 1 then
            ctrlBuyType.selectedIndex = 1
            txtDiamondCost.text = cfgData.f_GuildGiftPrice
        elseif cfgData.f_GuildGiftBuyTpye == 2 then
            ctrlBuyType.selectedIndex = 0
            txtRMBCost.text = string.format("￥%s购买", cfgData.f_GuildGiftPrice)
        end
        btnBuy.onClick:Clear()
        btnBuy.onClick:Add(function()
            if cfgData.f_GuildGiftBuyTpye == 1 then
                if logicMgr.ItemManager.getBagNumByTableID(CurrencyEnum.GOLD) >= cfgData.f_GuildGiftPrice then
                    costHandler.reqCostBuyItem(logicMgr.CostManager._GUILDGIFTID, {cfgData.f_GuildGiftId});
                else
                    UISysTips.AddMsg(getLanguage("Character_Tips7"));
                end
            elseif cfgData.f_GuildGiftBuyTpye == 2 then
                UIGuildGift:OnBuy(logicMgr.CostManager._GUILDGIFTID, cfgData.f_PayId, 1, cfgData.f_GuildGiftPrice, cfgData.f_GuildGiftId)
            end            
        end)
        btnGet.onClick:Clear()
        btnGet.onClick:Add(function()
            local data = {}
            table.insert(data, cfgData.f_GuildGiftId)
            logicMgr.CostManager.getRewardById(logicMgr.CostManager._GUILDGIFTID, data, function()
                if UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] then
                    utimer.remove(UIGuildGift.countDownTimer[cfgData.f_GuildGiftId])
                    UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] = nil
                end
            end, UIGuildGift)
        end)
        txtDesc.text = getLanguage(cfgData.f_GuildTaskDesc)
        logicMgr.CostManager.RefreshItemList(listItems, cfgData.f_GuildGiftReward)
        if srvData.state == 1 then
            UIGuildGift.countTime[cfgData.f_GuildGiftId] = (srvData.receiveTime/1000)+(cfgData.f_GuildGiftGetCd)/1000 - HelperFunc.GetServerTime()
            ctrlState.selectedIndex = 2
        elseif srvData.state == 2 then
            if isLeader then
                ctrlState.selectedIndex = 0
            else
                ctrlState.selectedIndex = 1
            end
            UIGuildGift.countTime[cfgData.f_GuildGiftId] = (srvData.completeTime/1000)+(cfgData.f_GuildGiftPassCd)/1000 - HelperFunc.GetServerTime()
        else
            printError("不该显示这个礼包")
        end

        if UIGuildGift.countTime[cfgData.f_GuildGiftId] > 0 then
            if UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] then
                utimer.remove(UIGuildGift.countDownTimer[cfgData.f_GuildGiftId])
                UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] = nil
            end
            if srvData.state == 1 then
                UIGuildGift.txtCountDown[cfgData.f_GuildGiftId].text = string.format(getLanguage("GuildGiftGetTime"), HelperFunc.GetTimeStr(UIGuildGift.countTime[cfgData.f_GuildGiftId]))
            elseif srvData.state == 2 then
                UIGuildGift.txtCountDown[cfgData.f_GuildGiftId].text = string.format(getLanguage("GuildGiftBuyTime"), HelperFunc.GetTimeStr(UIGuildGift.countTime[cfgData.f_GuildGiftId]))
            end
            UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] =  utimer.timeCall(function()
                UIGuildGift.countTime[cfgData.f_GuildGiftId] = UIGuildGift.countTime[cfgData.f_GuildGiftId] - 1
                if UIGuildGift.countTime[cfgData.f_GuildGiftId] <= 0 then
                    UIGuildGift.countTime[cfgData.f_GuildGiftId] = 0
                    logicMgr.GuildManager.SetGuildGiftState(cfgData.f_GuildGiftId,0)
                    utimer.remove(UIGuildGift.countDownTimer[cfgData.f_GuildGiftId])
                    UIGuildGift.countDownTimer[cfgData.f_GuildGiftId] = nil
                else
                    if srvData.state == 1 then
                        UIGuildGift.txtCountDown[cfgData.f_GuildGiftId].text = string.format(getLanguage("GuildGiftGetTime"), HelperFunc.GetTimeStr(UIGuildGift.countTime[cfgData.f_GuildGiftId]))
                    elseif srvData.state == 2 then
                        UIGuildGift.txtCountDown[cfgData.f_GuildGiftId].text = string.format(getLanguage("GuildGiftBuyTime"), HelperFunc.GetTimeStr(UIGuildGift.countTime[cfgData.f_GuildGiftId]))
                    end
                end
            end, 1000, -1)
        end
    end
end

-- 购买
function UIGuildGift:OnBuy(activeId, payId, num, cheekItemMoney, giftId)
    local indexs = {}
    table.insert(indexs, giftId)
    logicMgr.CostManager.getRewardByIdAndIndex(logicMgr.CostManager._GUILDGIFTID, indexs, payId, num, cheekItemMoney, function() self:onBuySuccess(giftId) end, self, nil)
end

function UIGuildGift:onBuySuccess(giftId)
    if self.countDownTimer[giftId] then
        utimer.remove(self.countDownTimer[giftId])
        self.countDownTimer[giftId] = nil
    end
    local dic = {}
    dic["guildgift_id"] = giftId
    TAManager.TrackByDictionary("buy_guildgift", dic)
end

function UIGuildGift:Refresh()
    self.curIndex = 1
    self.srvData = logicMgr.GuildManager.GetShowGiftData()
    self.listGiftItemCount = #self.srvData
    if self.listGiftItemCount > 0 then
        self.listGiftItem.numItems = self.listGiftItemCount
        self:UpdatePage()
    else
        self:ClickClose()
    end
end

function UIGuildGift:UpdatePage()
    if self.curIndex > 1 and self.listGiftItemCount > 1 then
        self.ctrlShowLeft.selectedIndex = 1
        self.txtBack.text = self.curIndex - 1
    else
        self.ctrlShowLeft.selectedIndex = 0
    end

    if self.curIndex < self.listGiftItemCount and self.listGiftItemCount > 1 then
        self.ctrlShowRight.selectedIndex = 1
        self.txtNext.text = self.listGiftItemCount - self.curIndex
    else
        self.ctrlShowRight.selectedIndex = 0
    end
end

function UIGuildGift:OnScrollToNext(evt)
    if self.curIndex < self.listGiftItemCount and self.listGiftItemCount > 1 then
        self.listGiftItem.scrollPane:ScrollRight(1, true)
        self.curIndex = self.curIndex + 1
        self:UpdatePage()
    end
end

function UIGuildGift:OnScrollToLast(evt)
    if self.curIndex > 1 and self.listGiftItemCount > 1 then
        self.listGiftItem.scrollPane:ScrollLeft(1, true)
        self.curIndex = self.curIndex - 1
        self:UpdatePage()
    end
end

function UIGuildGift:ClickClose()
    for k,v in pairs(self.countDownTimer) do
        utimer.remove(v)
        v= nil
    end
    self.listGiftItem.scrollPane.onScrollEnd:Clear()
    closeUI(PanelNames.UIGuildGift)
end
return UIGuildGift